/*
 * Editor.java
 *
 * Created on December 12, 2007, 2:20 PM
 */

package editor;


import imageViewer.ImageViewer;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.LinkedList;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import javax.swing.JOptionPane;
import oracle.ord.im.*;
import oracle.spatial.geometry.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
//import static ObjectInfo.Types.*;

/**
 * 
 * @author Martin Demin xdemin00, Eduard Benes xbenes29
 */
public class Editor extends javax.swing.JDialog {
    private BufferedImage myImage;
    private MyCanvas canvas;
    private LinkedList <Object>objects;
    private Dimension sceneSize;
    Object adding;
    Object selected;
    int accidentId;
    String name;
    MyCanvas thumbCanvas;
    java.awt.Frame parent;
    Point2D lastClick;
    java.awt.event.MouseEvent lastEvt;
    /** Creates new form Editor */
    public Editor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        jMenu1.getPopupMenu().setDefaultLightWeightPopupEnabled(false);
        Dimension d = jPanel1.getSize();
        myImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        jDialogProps.setSize(new Dimension(400,350));
        adding = selected = null;
        accidentId = -1;
        name = null;
        updatePropEditor();
        canvas = new MyCanvas();
        lastClick = new Point2D.Float(0,0);
        thumbCanvas = new MyCanvas();
        jPanel8.add(thumbCanvas);
        canvas.setImage(myImage);
        lastEvt = null;
        
        this.setTitle("Accident Editor");
        
        jTabbedPane1.setTitleAt(0, "Object");
        jTabbedPane1.setTitleAt(1, "Road");
        jTabbedPane1.setTitleAt(2, "Car");
        jTabbedPane1.setTitleAt(3, "Witness");
        jTabbedPane1.setTitleAt(4, "Obstacle");
        jTabbedPane1.setTitleAt(5, "Injured");
        
        canvas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                pressedMouse(evt);
            }
        });
        
        canvas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                releasedMouse(evt);
            }
        });
        
        canvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                draggedMouse(evt);
            }
        });
        
        canvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                movedMouse(evt);
            }
        });
        
        
        jPanel1.add(canvas);
        objects = new LinkedList<Object>();
        
        
        sceneSize = new Dimension(200,200);
        
        redraw();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jDialogProps = new javax.swing.JDialog();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jButton3 = new javax.swing.JButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jTextArea4 = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jCheckBox1 = new javax.swing.JCheckBox();
        jToggleButtonPoly = new javax.swing.JToggleButton();
        jToggleButtonLine = new javax.swing.JToggleButton();
        jToggleButtonPoint = new javax.swing.JToggleButton();
        jPanel1 = new javax.swing.JPanel();
        jToggleButtonAdd = new javax.swing.JToggleButton();
        jButton1 = new javax.swing.JButton();
        jToggleButtonCircle = new javax.swing.JToggleButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuItemSave = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jCheckBoxMenuItemProps = new javax.swing.JCheckBoxMenuItem();

        jDialogProps.setTitle("Object Editor");
        jDialogProps.setResizable(false);
        jDialogProps.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                jDialogPropsComponentHidden(evt);
            }
        });

        jRadioButton1.setText("Road");
        jRadioButton1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("Car");
        jRadioButton2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton2.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jRadioButton3.setText("Witness");
        jRadioButton3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton3.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        jRadioButton4.setText("Object/Obstacle");
        jRadioButton4.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton4.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        jButton3.setText("Update");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jRadioButton5.setText("Injured");
        jRadioButton5.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton5.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton1)
                            .addComponent(jRadioButton2)
                            .addComponent(jRadioButton3)
                            .addComponent(jRadioButton4)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(142, 142, 142)
                        .addComponent(jButton3))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jRadioButton5)))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton5)
                .addGap(19, 19, 19)
                .addComponent(jButton3)
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab("tab1", jPanel2);

        jLabel1.setText("Name:");

        jTextField1.setText("name");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(165, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(229, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab("tab2", jPanel3);

        jLabel9.setText("Driver:");

        jTextField6.setText("Stefan Mrkva");

        jLabel10.setText("ID:");

        jTextField7.setText("NZ111BB");

        jLabel7.setText("Type:");

        jTextField5.setText("Type");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(159, 159, 159))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(155, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab("tab3", jPanel4);

        jButton2.setText("Image");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Name:");

        jTextField4.setText("name");

        jLabel5.setText("ID:");

        jTextField8.setText("ID");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel8.setText("Image description");

        jLabel11.setText("Image name:");

        jTextField9.setText("Name");

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 89, Short.MAX_VALUE)
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 87, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(27, 27, 27)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField8)
                            .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButton2)
                                .addComponent(jLabel11)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(jTextField9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(11, 11, 11))
                            .addComponent(jScrollPane1))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(44, 44, 44)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(17, 17, 17))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16)))
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab("tab4", jPanel5);

        jLabel6.setText("Descpription:");

        jTextArea4.setColumns(20);
        jTextArea4.setRows(5);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jTextArea4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(139, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextArea4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(160, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab("tab5", jPanel6);

        jLabel2.setText("Name:");

        jTextField2.setText("name");

        jLabel3.setText("ID:");

        jTextField3.setText("ID");

        jCheckBox1.setText("Survived");
        jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox1.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1)
                    .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField3)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)))
                .addContainerGap(175, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox1)
                .addContainerGap(183, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab("tab6", jPanel7);

        javax.swing.GroupLayout jDialogPropsLayout = new javax.swing.GroupLayout(jDialogProps.getContentPane());
        jDialogProps.getContentPane().setLayout(jDialogPropsLayout);
        jDialogPropsLayout.setHorizontalGroup(
            jDialogPropsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogPropsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        jDialogPropsLayout.setVerticalGroup(
            jDialogPropsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogPropsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jToggleButtonPoly.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editor/icons/crossroads.png")));
        jToggleButtonPoly.setText("Poly");
        jToggleButtonPoly.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jToggleButtonPoly.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jToggleButtonPoly.setMaximumSize(new java.awt.Dimension(75, 30));
        jToggleButtonPoly.setMinimumSize(new java.awt.Dimension(75, 30));
        jToggleButtonPoly.setPreferredSize(new java.awt.Dimension(75, 30));
        jToggleButtonPoly.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonPolyActionPerformed(evt);
            }
        });

        jToggleButtonLine.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editor/icons/object_barrier.png")));
        jToggleButtonLine.setText("Line");
        jToggleButtonLine.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jToggleButtonLine.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jToggleButtonLine.setMaximumSize(new java.awt.Dimension(75, 30));
        jToggleButtonLine.setMinimumSize(new java.awt.Dimension(75, 30));
        jToggleButtonLine.setPreferredSize(new java.awt.Dimension(75, 30));
        jToggleButtonLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonLineActionPerformed(evt);
            }
        });

        jToggleButtonPoint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editor/icons/witness.png")));
        jToggleButtonPoint.setText("Point");
        jToggleButtonPoint.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jToggleButtonPoint.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jToggleButtonPoint.setMaximumSize(new java.awt.Dimension(75, 30));
        jToggleButtonPoint.setMinimumSize(new java.awt.Dimension(75, 30));
        jToggleButtonPoint.setPreferredSize(new java.awt.Dimension(75, 30));
        jToggleButtonPoint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonPointActionPerformed(evt);
            }
        });

        jPanel1.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                jPanel1ComponentResized(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 597, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 477, Short.MAX_VALUE)
        );

        jToggleButtonAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editor/icons/add.png")));
        jToggleButtonAdd.setText("PolyAdd");
        jToggleButtonAdd.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jToggleButtonAdd.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jToggleButtonAdd.setMaximumSize(new java.awt.Dimension(75, 30));
        jToggleButtonAdd.setMinimumSize(new java.awt.Dimension(75, 30));
        jToggleButtonAdd.setPreferredSize(new java.awt.Dimension(75, 30));
        jToggleButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonAddActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editor/icons/delete.png")));
        jButton1.setText("Delete");
        jButton1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jButton1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButton1.setMaximumSize(new java.awt.Dimension(75, 30));
        jButton1.setMinimumSize(new java.awt.Dimension(75, 30));
        jButton1.setPreferredSize(new java.awt.Dimension(75, 30));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jToggleButtonCircle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editor/icons/object_tree.png")));
        jToggleButtonCircle.setText("Circle");
        jToggleButtonCircle.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jToggleButtonCircle.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jToggleButtonCircle.setMaximumSize(new java.awt.Dimension(75, 30));
        jToggleButtonCircle.setMinimumSize(new java.awt.Dimension(75, 30));
        jToggleButtonCircle.setPreferredSize(new java.awt.Dimension(75, 30));
        jToggleButtonCircle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonCircleActionPerformed(evt);
            }
        });

        jLabel12.setText("Status bar:");

        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel13.setText("jLabel13");

        jMenu1.setText("Menu");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItemExit.setText("Exit");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItemExit);

        jMenuItemSave.setText("Save");
        jMenuItemSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItemSave);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Tools");
        jCheckBoxMenuItemProps.setText("Properties Tool");
        jCheckBoxMenuItemProps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxMenuItemPropsActionPerformed(evt);
            }
        });

        jMenu2.add(jCheckBoxMenuItemProps);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToggleButtonPoly, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToggleButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToggleButtonLine, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToggleButtonPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToggleButtonCircle, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addGap(94, 94, 94)
                .addComponent(jLabel13)
                .addContainerGap(372, Short.MAX_VALUE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jToggleButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButtonLine, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButtonPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButtonCircle, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButtonPoly, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Update the status bar
     */
    private void updateStatus() {
        int l = editor.ObjectInfo.Types.values().length;
        int []types = new int[l];
        String s = "Objs: ";
        if(objects == null) return;
        for (Integer elem : types) {
            elem = 0;
        }
        
        for (Object elem : objects) {
            types[elem.getInfo().getType().ordinal()] += 1;
        }

        for (int i=0; i<l; i++) {
            s += editor.ObjectInfo.Types.values()[i] + ": ";
            s += String.valueOf(types[i]) + "  ";
        }
        jLabel13.setText(s);
    }
    
    
    private void movedMouse(java.awt.event.MouseEvent evt) {
        String s = "";
        Dimension d = jPanel1.getSize();
        
        if(evt != null)
            lastEvt = evt;
        else
        {
            evt = lastEvt;
            if(evt == null) return;
        }
        DecimalFormat myFormatter = new DecimalFormat("000.0");
        s += "x: "+ String.valueOf(myFormatter.format( evt.getX() * (float)sceneSize.width/d.width )) ;
        s += " y: "+ String.valueOf(myFormatter.format( evt.getY() * (float)sceneSize.height/d.height)) ;
        s += " d: "+ String.valueOf(myFormatter.format( lastClick.distance(evt.getX() * (float)sceneSize.width/d.width,
                evt.getY() * (float)sceneSize.height/d.height))) ;
       jLabel12.setText(s); 
       Point p = jLabel13.getLocation();
       p.x = 100;
       jLabel13.setLocation(p);
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ImageViewer view = new ImageViewer(parent, true);
        view.setImage(selected.getInfo().image);
        view.setSize(400,400);
        view.setVisible(true);
        selected.getInfo().image = view.getImage();
        wittnessUpdateThumb(selected);
        view.dispose();
        this.updatePropEditor();
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        if(selected == null) return;
        myImage.toString();
        
        switch(selected.getInfo().getType().ordinal()) {
        
            
            case 0:  selected.getInfo().name = jTextField1.getText(); break;
            
            case 1:  selected.getInfo().driver = jTextField6.getText();
                     selected.getInfo().brand = jTextField5.getText();  
                     selected.getInfo().carId = jTextField7.getText();  
                     break;

            case 2:  selected.getInfo().name = jTextField4.getText();
                     selected.getInfo().id = jTextField8.getText(); 
                     
                     if(selected.getInfo().image != null) {
                         selected.getInfo().imageName = jTextField9.getText();
                         selected.getInfo().imageD = jTextArea1.getText();
                     }
                     else
                     {

                     }
                     break;
            
            case 3:  selected.getInfo().name = jTextArea4.getText(); break;
            
            case 4:  selected.getInfo().name = jTextField2.getText();
                     selected.getInfo().id = jTextField3.getText();
                     selected.getInfo().surv = jCheckBox1.isSelected()?1:0; break;
            default:
            
                break;
            }
        
    }//GEN-LAST:event_jButton3ActionPerformed
    
    private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed
        selected.setInfo(new ObjectInfo(ObjectInfo.Types.INJ));
        updatePropEditor();
    }//GEN-LAST:event_jRadioButton5ActionPerformed
    
    private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed
        this.saveAccident();
    }//GEN-LAST:event_jMenuItemSaveActionPerformed
    
    /**
     * Load accident with id from database
     * @param gid gid present in the database
     */
    public void loadAccident(int gid) {
        try {
            this.accidentId = gid;
            this.loadAccidentTable();
            this.loadInjuredTable();
            this.loadObjectsTable();
            this.loadVehiclesTable();
            this.loadWhitnessTable();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,
                    "There was an error while loading scene :-(",
                    "Error",
                    JOptionPane.ERROR_MESSAGE );
        }
        
    }
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        jDialogProps.dispose();
    }//GEN-LAST:event_formWindowClosing
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(selected != null)
            objects.remove(selected);
        selected = null;
        updatePropEditor();
        redraw();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
        selected.setInfo(new ObjectInfo(ObjectInfo.Types.UNSPEC));
        updatePropEditor();
    }//GEN-LAST:event_jRadioButton4ActionPerformed
    
    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        selected.setInfo(new ObjectInfo(ObjectInfo.Types.WHIT));
        updatePropEditor();
    }//GEN-LAST:event_jRadioButton3ActionPerformed
    
    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        selected.setInfo(new ObjectInfo(ObjectInfo.Types.CAR));
        updatePropEditor();
    }//GEN-LAST:event_jRadioButton2ActionPerformed
    
    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        selected.setInfo(new ObjectInfo(ObjectInfo.Types.ROAD));
        updatePropEditor();
    }//GEN-LAST:event_jRadioButton1ActionPerformed
    
    private void jCheckBoxMenuItemPropsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItemPropsActionPerformed
        if(jCheckBoxMenuItemProps.isSelected())
        {
            jDialogProps.setVisible(true);
            this.updatePropEditor();
        }
        else
            jDialogProps.setVisible(false);
    }//GEN-LAST:event_jCheckBoxMenuItemPropsActionPerformed
    
    private void jDialogPropsComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jDialogPropsComponentHidden
        jCheckBoxMenuItemProps.setSelected(false);
    }//GEN-LAST:event_jDialogPropsComponentHidden
    
    private void jPanel1ComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel1ComponentResized
        Dimension d = jPanel1.getSize();
        myImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        canvas.setImage(myImage);
        Iterator <Object>i = objects.iterator();
        while(i.hasNext()) {
            i.next().setScale((float)d.width/sceneSize.width, (float)d.height/sceneSize.height);
        }
        redraw();
    }//GEN-LAST:event_jPanel1ComponentResized
    
    private void jToggleButtonCircleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonCircleActionPerformed
        if(jToggleButtonCircle.isSelected()) {
            jToggleButtonAdd.setSelected(false);
            jToggleButtonPoly.setSelected(false);
            jToggleButtonPoint.setSelected(false);
            jToggleButtonLine.setSelected(false);
        }
        unselectAll();
    }//GEN-LAST:event_jToggleButtonCircleActionPerformed
    
    private void jToggleButtonLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonLineActionPerformed
        if(jToggleButtonLine.isSelected()) {
            jToggleButtonAdd.setSelected(false);
            jToggleButtonPoly.setSelected(false);
            jToggleButtonPoint.setSelected(false);
            jToggleButtonCircle.setSelected(false);
        }
        unselectAll();
    }//GEN-LAST:event_jToggleButtonLineActionPerformed
    
    private void jToggleButtonPointActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonPointActionPerformed
        if(jToggleButtonPoint.isSelected()) {
            jToggleButtonAdd.setSelected(false);
            jToggleButtonPoly.setSelected(false);
            jToggleButtonLine.setSelected(false);
            jToggleButtonCircle.setSelected(false);
        }
        unselectAll();
    }//GEN-LAST:event_jToggleButtonPointActionPerformed
    
    private void jToggleButtonPolyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonPolyActionPerformed
        if(jToggleButtonPoly.isSelected()) {
            jToggleButtonAdd.setSelected(false);
            jToggleButtonPoint.setSelected(false);
            jToggleButtonLine.setSelected(false);
            jToggleButtonCircle.setSelected(false);
        }
        unselectAll();
    }//GEN-LAST:event_jToggleButtonPolyActionPerformed
    
    private void jToggleButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonAddActionPerformed
        if(jToggleButtonAdd.isSelected()) {
            jToggleButtonPoly.setSelected(false);
            jToggleButtonPoint.setSelected(false);
            jToggleButtonLine.setSelected(false);
            jToggleButtonCircle.setSelected(false);
        }
        unselectAll();
    }//GEN-LAST:event_jToggleButtonAddActionPerformed
    
    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_jMenu1ActionPerformed
    
    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        this.jDialogProps.dispose();
        this.dispose();
        
    }//GEN-LAST:event_jMenuItemExitActionPerformed
    
    /**
     * Redraw the accident window
     */
    public void redraw() {
        Iterator <Object>i = objects.iterator();
        Object o;
        myImage.getGraphics().clearRect(0,0, myImage.getWidth(), myImage.getHeight());
        while(i.hasNext()) {
            o = i.next();
            o.draw(myImage.getGraphics());
        }
        if(adding != null) adding.draw(myImage.getGraphics());
        canvas.update(canvas.getGraphics());
        this.updateStatus();
    }
    
    
    /**
     * Unselect all the object in the scene
     */
    private void unselectAll() {
        selected = null;
        updatePropEditor();
        if(adding != null) {
            if(adding.isComplete()) {
                
                objects.add(adding);
                adding = null;
            } else {
                adding = null;
            }
        }
        Iterator <Object>i = objects.iterator();
        while(i.hasNext()) {
            i.next().unselect();
        }
        redraw();
    }
    
    /**
     * Called when the mouse's button is pressed
     */
    private void pressedMouse(java.awt.event.MouseEvent evt) {
        // go through all object, find one we have selected
        Iterator <Object>i = objects.iterator();
        Object o;
        int x = evt.getX(), y = evt.getY();
        if(x < 0) x = 0;
        if(x > myImage.getWidth()) x = myImage.getWidth();
        if(y < 0) y = 0;
        if(y > myImage.getHeight()) y = myImage.getHeight();
        
        // unselect all
        Dimension d = jPanel1.getSize();
        lastClick.setLocation(x * (float)sceneSize.width/d.width, y * (float)sceneSize.height/d.height);
        movedMouse(null);
        
        if(jToggleButtonAdd.isSelected()) { // we are going to add a point to a polygon
            
            while(i.hasNext()) { // go through all objects
                o = i.next();
                if(o.getType() == 0) {// if object is polygon
                    if(((ObjectPolygon)o).doAddPoint(x, y))
                        break;
                }
            }
            
            redraw();
            return;
        }
        
        if(jToggleButtonPoly.isSelected() || jToggleButtonPoint.isSelected()
        || jToggleButtonLine.isSelected() || jToggleButtonCircle.isSelected()) { // we are going to add a point to a polygon
            if(adding == null && jToggleButtonPoly.isSelected()) {
                adding = new ObjectPolygon();
                if(adding.getType()==0) ((ObjectPolygon)adding).setStrict(true);
                adding.setInfo(new ObjectInfo(editor.ObjectInfo.Types.UNSPEC));
            }
            if(adding == null && jToggleButtonPoint.isSelected()) {
                adding = new ObjectPoint();
                adding.setInfo(new ObjectInfo(editor.ObjectInfo.Types.INJ));
            }
            if(adding == null && jToggleButtonLine.isSelected()) {
                adding = new ObjectLine();
                adding.setInfo(new ObjectInfo(editor.ObjectInfo.Types.UNSPEC));
            }
            if(adding == null && jToggleButtonCircle.isSelected()) {
                adding = new ObjectCircle();
                adding.setInfo(new ObjectInfo(editor.ObjectInfo.Types.UNSPEC));
            }
            adding.select();
            // SET SCALE HERE
            adding.setScale((float)myImage.getWidth()/sceneSize.width, (float)myImage.getHeight()/sceneSize.height);
            adding.addPoint(x, y);
            
            if(adding.getType() == 3 && !adding.canAdd())
                adding.setInfo(new ObjectInfo(editor.ObjectInfo.Types.WHIT));
            
            updatePropEditor();
            if(!adding.canAdd())  { // check if there can be any points added
                adding.unselect();
                objects.add(adding); // if not, merge the object
                adding = null;
                selected = null;
            }
            redraw();
            return;
        }
        
        
        unselectAll();
        // now find the one, that was clicked
        i = objects.iterator();
        while(i.hasNext()) {
            o = i.next();
            if(o.doSelected(x, y)) {
                selected = o;
                this.wittnessUpdateThumb(o);
                updatePropEditor();
                break;
            }
        }
        redraw();
        
    }
    
    /**
     * Called when the mouse's cursor is dragged
     */
    private void draggedMouse(java.awt.event.MouseEvent evt) {
        Iterator <Object>i = objects.iterator();
        Object o = null;
        int x = evt.getX(), y = evt.getY();
        if(x < 0) x = 0;
        if(x > myImage.getWidth()) x = myImage.getWidth();
        if(y < 0) y = 0;
        if(y > myImage.getHeight()) y = myImage.getHeight();
        // unselect all
        while(i.hasNext()) {
            o = i.next();
            if(o.isSelected()) {
                selected = o;
                updatePropEditor();
                break;
            }
        }
        if( o == null) return;
        o.doDragged(x, y);
        redraw();
    }
    
    /**
     * Called when the mouse's button is released
     */
    private void releasedMouse(java.awt.event.MouseEvent evt) {
        Iterator <Object>i = objects.iterator();
        Object o = null;
        
        // unselect all
        while(i.hasNext()) {
            o = i.next();
            if(o.isSelected()) {
                selected = o;
                break;
            }
        }
        if( o == null) return;
        o.doReleased();
        updatePropEditor();
    }
    
    /**
     * Update Dialog with properties
     */
    private void updatePropEditor() {
        updateStatus();
        if(!jDialogProps.isVisible()) return;
        if(selected == null){
//            for(int i=0;i<jTabbedPane1.getTabCount();i++) {
            jTabbedPane1.setEnabled(false);
            jTabbedPane1.setSelectedIndex(0);
            jRadioButton1.setEnabled(false);
            jRadioButton2.setEnabled(false);
            jRadioButton3.setEnabled(false);
            jRadioButton4.setEnabled(false);
            jRadioButton5.setEnabled(false);
//            }
        } else {
            jTabbedPane1.setEnabled(true);
            for(int i=1;i<jTabbedPane1.getTabCount();i++) {
                jTabbedPane1.setEnabledAt(i,false);
            }
            jRadioButton1.setSelected(false);
            jRadioButton2.setSelected(false);
            jRadioButton3.setSelected(false);
            jRadioButton4.setSelected(false);
            jRadioButton5.setSelected(false);
            
            if(selected.getType() == 0)
                jRadioButton1.setEnabled(true); // ROAD
            if(selected.getType() == 0)
                jRadioButton2.setEnabled(true); // CAR

            if(selected.getType() == 3 && !selected.canAdd())
                jRadioButton3.setEnabled(true); // WITNESS
            
            if(selected.getType() != 3 )
                jRadioButton4.setEnabled(true); // UNSPEC

            if(selected.getType() == 3 && selected.canAdd())
                jRadioButton5.setEnabled(true); // INJURED
            
            switch(selected.getInfo().getType().ordinal()) {
                case 0: jRadioButton1.setSelected(true); jTabbedPane1.setEnabledAt(1,true); 
                        jTextField1.setText(selected.getInfo().name); break;
                case 1: jRadioButton2.setSelected(true); jTabbedPane1.setEnabledAt(2,true); 
                        jTextField6.setText(selected.getInfo().driver); 
                        jTextField7.setText(selected.getInfo().carId); 
                        jTextField5.setText(selected.getInfo().brand); break;
                case 2: jRadioButton3.setSelected(true); jTabbedPane1.setEnabledAt(3,true); 
                        jTextField4.setText(selected.getInfo().name); 
                        jTextField8.setText(selected.getInfo().id); 
                         if(selected.getInfo().image != null) {
                             jTextField9.setText(selected.getInfo().imageName);
                             jTextArea1.setText(selected.getInfo().imageD);
                             if(selected.getInfo().imageThumb != null){
                                 thumbCanvas.setImage(selected.getInfo().imageThumb);
                                 thumbCanvas.update(thumbCanvas.getGraphics());
                             }
                             else
                             {
                                 thumbCanvas.setImage(new BufferedImage(64,64,BufferedImage.TYPE_INT_RGB));
                                 thumbCanvas.update(thumbCanvas.getGraphics());
                             }

                         }
                         else
                         {
                             jTextField9.setText("");
                             jTextArea1.setText("");
                             thumbCanvas.setImage(new BufferedImage(64,64,BufferedImage.TYPE_INT_RGB));
                             thumbCanvas.update(thumbCanvas.getGraphics());
                         }
                        break;
                case 3: jRadioButton4.setSelected(true); jTabbedPane1.setEnabledAt(4,true); 
                        jTextArea4.setText(selected.getInfo().name); break;
                case 4: jRadioButton5.setSelected(true); jTabbedPane1.setEnabledAt(5,true); 
                        jTextField2.setText(selected.getInfo().name); 
                        jTextField3.setText(selected.getInfo().id); 
                        jCheckBox1.setSelected(selected.getInfo().surv != 0); break;
                default:
                    break;
            }
        }
    }
    
    /**
     * Save the accident into the DB
     */
    private void saveAccident() {
        int c = 0;
        
        for (Object elem : objects) {
            if(elem.getInfo().getType() == editor.ObjectInfo.Types.ROAD) c++;
        }
        if(c!=1)
        {
            JOptionPane.showMessageDialog(this,
            "The Site must contain EXACTLY one ROAD !",
            "Insane",
            JOptionPane.ERROR_MESSAGE );
            return;
        }
        
        Iterator <Object>i = objects.iterator();
        Object o = null;
        try {

            if(accidentId == -1)
            {
                try {
                    OracleResultSet res = (OracleResultSet) sqlComm.SqlComm.getInstance().doQuery("SELECT seq_accidents.nextval FROM accidents WHERE ROWNUM <=1");
                    res.next();
                    accidentId = res.getInt(1);
                }
                catch (Exception e) {
                    //accidentId = 1;
                }
            }
            else
            {
                // now delete the line
                try {
                    OracleResultSet res = (OracleResultSet) sqlComm.SqlComm.getInstance().doQuery("DELETE FROM accidents WHERE gid = " + String.valueOf(accidentId));
                }
                catch (Exception e) {
                    
                }
            }

            String s = "insert into accidents values(";
            if(accidentId != -1)
                s += String.valueOf(accidentId);
            else
                s += "seq_accidents.nextval";
            s += ", '";
            while(i.hasNext() ) {
                o = i.next();
                if(o.getInfo().getType() == editor.ObjectInfo.Types.ROAD)
                    break;
            }
            if(o != null && o.getInfo().getType() != editor.ObjectInfo.Types.ROAD) return;
            s += o.getInfo().name;
            s+= "',";
            s += o.getGeom();
            s += ',';
            s += getVehiclesTable();
            s += ',';
            s += getWhitnessTable();
            s += ',';
            s += getInjuredTable();
            s += ',';
            s += getObjectsTable();
            s += ")";
            sqlComm.SqlComm.getInstance().doQuery(s);
            sqlComm.SqlComm.getInstance().getConn().commit();
            sqlComm.SqlComm.getInstance().getConn().setAutoCommit(false);
            // now looop through all the images and update them
            //update table (select witness from accidents where accidents.name = 'krizovatka1') w set w.image  = image_t( 'aaaaa','popisek',NULL,NULL) where w.pin = '720604/1234';
            
            for (Object elem : objects) {
                if(elem.getInfo().getType() == editor.ObjectInfo.Types.WHIT && elem.getInfo().image != null) {

                    OraclePreparedStatement stmt = (OraclePreparedStatement) sqlComm.SqlComm.getInstance().getConn().prepareStatement(
                            "SELECT w.image.image, w.image.signature FROM accidents c, table(c.witness) w WHERE c.gid = ? AND w.pin = ? FOR UPDATE");
                    stmt.setInt(1, accidentId);
                    stmt.setString(2, elem.getInfo().id);
                    OracleResultSet res = (OracleResultSet) stmt.executeQuery();
                    res.next();
                    OrdImage image = (OrdImage)res.getCustomDatum(1,OrdImage.getFactory());
                    OrdImageSignature signature = (OrdImageSignature)res.getCustomDatum(2, OrdImageSignature.getFactory());
                    
                            
                    OraclePreparedStatement stmt1 = (OraclePreparedStatement) sqlComm.SqlComm.getInstance().getConn().prepareStatement(
                            "update table (select witness from accidents where accidents.gid = ?) w set w.image  = image_t( ?, ?, ?, ?) where w.pin = ?");
                    stmt1.setInt(1, accidentId);
                    stmt1.setString(2,elem.getInfo().imageName);
                    stmt1.setString(3,elem.getInfo().imageD);
                    
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    MemoryCacheImageOutputStream mos = new MemoryCacheImageOutputStream( baos );
                    imageViewer.ImageViewer.doImage( elem.getInfo().image, mos );
                    byte []array = baos.toByteArray();
                    
//                    image = new OrdImage();
                    image.loadDataFromByteArray(array);
                    
                    stmt1.setCustomDatum( 4, image );
                    signature.generateSignature(image);
                    stmt1.setCustomDatum( 5, signature );
                    
                    stmt1.setString(6, elem.getInfo().id);
                    
                    /*res = (OracleResultSet)*/ stmt1.executeQuery();
                    
                }
            }
            
            sqlComm.SqlComm.getInstance().getConn().commit();
            sqlComm.SqlComm.getInstance().getConn().setAutoCommit(true);
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }
  
  
    
    /**
     * Creates the String with the SQL command to insert Vehicles into the DB
     */
    private String getVehiclesTable() {
        String s = "";
        int count = 0;
        Iterator <Object>i = objects.iterator();
        Object o;
        
        while(i.hasNext()) {
            o = i.next();
            if(o.getInfo().getType() == editor.ObjectInfo.Types.CAR) {
                if(count != 0)
                    s += ',';
                else
                    s += "vehicles_n(";
                s += "vehicles_t(\'";
                s += o.getInfo().carId+"\',\'";
                s += o.getInfo().brand+"\',\'";
                s += o.getInfo().driver+"\',";
                s += o.getGeom();
                s += ")";
                count ++;
            }
        }
        
        s += ")";
        if(count == 0) return "null";
        return s;
    }
    
    /**
     * Creates the String with the SQL command to insert Witness into the DB
     */
    private String getWhitnessTable() {
        String s = "";
        int count = 0;
        Iterator <Object>i = objects.iterator();
        Object o;
        
        while(i.hasNext()) {
            o = i.next();
            if(o.getInfo().getType() == editor.ObjectInfo.Types.WHIT) {
                if(count != 0)
                    s += ',';
                else
                    s += "witness_n(";
                s += "witness_t(\'";
                s += o.getInfo().id+"\',\'";
                s += o.getInfo().name+"\',";
                s += o.getGeom();
                s += ", image_t( 'null','null',ORDSYS.ORDImage.init(), ORDSYS.ORDImageSignature.init())";
                s += ")";
                count ++;
            }
        }
        
        s += ")";
        if(count == 0) return "null";
        return s;
    }
    
    /**
     * Creates the String with the SQL command to insert Injured into the DB
     */
    private String getInjuredTable() {
        String s = "";
        int count = 0;
        Iterator <Object>i = objects.iterator();
        Object o;
        
        while(i.hasNext()) {
            o = i.next();
            if(o.getInfo().getType() == editor.ObjectInfo.Types.INJ) {
                if(count != 0)
                    s += ',';
                else
                    s += "injured_n(";
                s += "injured_t(\'";
                s += o.getInfo().id+"\',\'";
                s += o.getInfo().name+"\',";
                s += String.valueOf(o.getInfo().surv)+",";
                s += o.getGeom();
                s += ")";
                count ++;
            }
        }
        
        s += ")";
        if(count == 0) return "null";
        return s;
    }
    /**
     * Creates the String with the SQL command to insert Objects into the DB
     */
    private String getObjectsTable() {
        String s = "";
        OraclePreparedStatement a;
        //a.
        int count = 0;
        Iterator <Object>i = objects.iterator();
        Object o;
        
        while(i.hasNext()) {
            o = i.next();
            if(o.getInfo().getType() == editor.ObjectInfo.Types.UNSPEC) {
                if(count != 0)
                    s += ',';
                else
                    s += "objects_n(";
                s += "objects_t(seq_objects.nextval,\'";
                //s += o.getInfo().id+"\',\'";
                s += o.getInfo().name+"\',";
                s += o.getGeom();
                s += ")";
                count ++;
            }
        }
        
        s += ")";
        if(count == 0) return "null";
        return s;
    }
    
    
    void loadVehiclesTable() throws SQLException {
        // select v.spz, v.driver, v.geometry.SDO_ORDINATES from accidents c, table(c.vehicles) v where c.gid = '1';
        JGeometry jGeo ;
        OraclePreparedStatement stmt = (OraclePreparedStatement) sqlComm.SqlComm.getInstance().getConn().prepareStatement("SELECT v.spz, v.type, v.driver, v.geometry FROM accidents c, table(c.vehicles) v WHERE c.gid = ?");
        stmt.setString(1, String.valueOf(accidentId));
        OracleResultSet res = (OracleResultSet) stmt.executeQuery();
        
        while(res.next()) {
            Object o = null;
            jGeo = JGeometry.load(res.getSTRUCT(4));
            if(jGeo.getType() == 3)
                o = new ObjectPolygon();
            o.setInfo(new ObjectInfo(ObjectInfo.Types.CAR));
            o.getInfo().carId = res.getString(1);
            o.getInfo().brand = res.getString(2);
            o.getInfo().driver = res.getString(3);
            double[] points = (double[]) jGeo.getOrdinatesOfElements()[0];
            for(int i=0;i<points.length-2;i+=2) {
                ((ObjectPolygon)o).addPoint(new Point2D.Double(points[i],points[i+1]));
            }
            if(o.getType()==0) ((ObjectPolygon)o).setStrict(true);
            objects.add(o);
        }
        //redraw();
        
        
    }
    
    /**
     * Update the thumbnail
     */
    private void wittnessUpdateThumb(Object o) {
        if(o.getInfo().image == null)
        {
            o.getInfo().imageThumb = null;
            return;
        }
            
        int srcHeight = o.getInfo().image.getHeight();
        int srcWidth = o.getInfo().image.getWidth();

        // Constrain the thumbnail to a predefined box size
        int height = 64;
        int width = 64;
        if( srcHeight > srcWidth )
        {
                    width = ( int )( ( ( float )height / ( float )srcHeight ) * ( float )srcWidth );
        }
        else if( srcWidth > srcHeight )
        {
            height = ( int )( ( ( float )width / ( float )srcWidth ) * ( float )srcHeight );
        }

        // Create a new thumbnail BufferedImage
        o.getInfo().imageThumb = new BufferedImage( width, height, BufferedImage.TYPE_USHORT_565_RGB );
        Graphics g = o.getInfo().imageThumb.getGraphics();
        g.drawImage( o.getInfo().image, 0, 0, width, height, null );
        
    }
    
    void loadWhitnessTable() throws SQLException {
        // select w.pin, w.name, w.geometry.SDO_ORDINATES from accidents c, table(c.witness) w where c.gid = '1';
        try {
        
            JGeometry jGeo ;
            OraclePreparedStatement stmt = (OraclePreparedStatement) sqlComm.SqlComm.getInstance().getConn().prepareStatement("SELECT w.pin, w.name, w.geometry, w.image.image, w.image.name, w.image.description FROM accidents c, table(c.witness) w WHERE c.gid = ?");
            stmt.setString(1, String.valueOf(accidentId));
            OracleResultSet res = (OracleResultSet) stmt.executeQuery();

            while(res.next()) {
                  Object o = null;
                  jGeo = JGeometry.load(res.getSTRUCT(3));
                  if(jGeo.getType() == 1)
                      o = new ObjectPoint();
                  o.setInfo(new ObjectInfo(ObjectInfo.Types.WHIT));
                  o.getInfo().name = res.getString(2);
                  o.getInfo().id = res.getString(1);

                  OrdImage image = (OrdImage)res.getCustomDatum(4,OrdImage.getFactory());
                  Blob blob = (java.sql.Blob)image.getContent();
                  InputStream in = blob.getBinaryStream();
                      o.getInfo().image = ImageIO.read( in );

                  o.getInfo().imageName = res.getString(5);
                  o.getInfo().imageD = res.getString(6);

                  wittnessUpdateThumb(o);


                ((ObjectPoint)o).addPoint(new Point2D.Double(jGeo.getFirstPoint()[0],jGeo.getFirstPoint()[1]));
                ((ObjectPoint)o).addPoint(new Point2D.Double(jGeo.getLastPoint()[0],jGeo.getLastPoint()[1]));
                objects.add(o);
            }
            //redraw();

        } catch (Exception ex) {
            JGeometry jGeo ;
            OraclePreparedStatement stmt = (OraclePreparedStatement) sqlComm.SqlComm.getInstance().getConn().prepareStatement("SELECT w.pin, w.name, w.geometry FROM accidents c, table(c.witness) w WHERE c.gid = ?");
            stmt.setString(1, String.valueOf(accidentId));
            OracleResultSet res = (OracleResultSet) stmt.executeQuery();

            while(res.next()) {
                Object o = null;
                jGeo = JGeometry.load(res.getSTRUCT(3));
                if(jGeo.getType() == 1)
                    o = new ObjectPoint();
                o.setInfo(new ObjectInfo(ObjectInfo.Types.WHIT));
                o.getInfo().name = res.getString(2);
                o.getInfo().id = res.getString(1);

                o.getInfo().imageName = "";
                o.getInfo().imageD = "";
                o.getInfo().image = null;

                ((ObjectPoint)o).addPoint(new Point2D.Double(jGeo.getFirstPoint()[0],jGeo.getFirstPoint()[1]));
                ((ObjectPoint)o).addPoint(new Point2D.Double(jGeo.getLastPoint()[0],jGeo.getLastPoint()[1]));
                objects.add(o);
            }
            //redraw();
        }
        
        
        
    }
    void loadObjectsTable() throws SQLException {
        // select o.id, o.type, o.geometry.SDO_ORDINATES from accidents c, table(c.objects) o where c.gid = '1';
        
        JGeometry jGeo ;
        OraclePreparedStatement stmt = (OraclePreparedStatement) sqlComm.SqlComm.getInstance().getConn().prepareStatement("select o.type, o.geometry from accidents c, table(c.objects) o where c.gid = ?");
        stmt.setString(1, String.valueOf(accidentId));
        OracleResultSet res = (OracleResultSet) stmt.executeQuery();
        
        while(res.next()) {
            Object o = null;
            jGeo = JGeometry.load(res.getSTRUCT(2));
            if(jGeo.getType() == 3)
            {
                if(jGeo.getElemInfo().length*2 != jGeo.getOrdinatesArray().length)
                    o = new ObjectPolygon();
                else
                {
                    o = new ObjectCircle();
                    ((ObjectCircle)o).addPoints(jGeo.getOrdinatesArray());
                    o.setInfo(new ObjectInfo(ObjectInfo.Types.UNSPEC));
                    o.getInfo().name = res.getString(1);
                    objects.add(o);
                    continue;
                }
                    
            }
            else
                if(jGeo.getType() == 2)
                    o = new ObjectLine();
                else
                    if(jGeo.getType() == 1)
                        continue;
            double[] points = (double[]) jGeo.getOrdinatesOfElements()[0];
            for(int i=0;i<points.length;i+=2) {

                o.addPoint(new Point2D.Double(points[i],points[i+1]));
            }
            o.setInfo(new ObjectInfo(ObjectInfo.Types.UNSPEC));
            o.getInfo().name = res.getString(1);
            if(o.getType()==0) ((ObjectPolygon)o).setStrict(true);
            objects.add(o);
        }
        //redraw();
        
        
    }
    void loadInjuredTable() throws SQLException {
        // select w.pin, w.name, w.geometry.SDO_ORDINATES from accidents c, table(c.witness) w where c.gid = '1';
        JGeometry jGeo ;
        OraclePreparedStatement stmt = (OraclePreparedStatement) sqlComm.SqlComm.getInstance().getConn().prepareStatement("select w.pin, w.name, w.survive, w.geometry from accidents c, table(c.injured) w where c.gid = ?");
        stmt.setString(1, String.valueOf(accidentId));
        OracleResultSet res = (OracleResultSet) stmt.executeQuery();
        
        while(res.next()) {
            Object o = null;
            jGeo = JGeometry.load(res.getSTRUCT(4));
            if(jGeo.getType() == 1)
                o = new ObjectPoint();
            o.setInfo(new ObjectInfo(ObjectInfo.Types.INJ));
            o.getInfo().name = res.getString(2);
            o.getInfo().id = res.getString(1);
            o.getInfo().surv = res.getInt(3);
            ((ObjectPoint)o).addPoint(new Point2D.Double(jGeo.getFirstPoint()[0],jGeo.getFirstPoint()[1]));
            //((ObjectPoint)o).addPoint(new Point2D.Double(jGeo.getLastPoint()[0],jGeo.getLastPoint()[1]));
            objects.add(o);
        }
        //redraw();
        
        
    }
    
    //select c.gid, c.name, c.geometry.SDO_ORDINATES from accidents c where c.gid = '1';
    void loadAccidentTable() throws SQLException {
        // select v.spz, v.driver, v.geometry.SDO_ORDINATES from accidents c, table(c.vehicles) v where c.gid = '1';
        JGeometry jGeo ;
        OraclePreparedStatement stmt = (OraclePreparedStatement) sqlComm.SqlComm.getInstance().getConn().prepareStatement("select c.name, c.geometry from accidents c where c.gid = ?");
        stmt.setString(1, String.valueOf(accidentId));
        OracleResultSet res = (OracleResultSet) stmt.executeQuery();
        
        while(res.next()) {
            Object o = null;
            jGeo = JGeometry.load(res.getSTRUCT(2));
            if(jGeo.getType() == 3)
                o = new ObjectPolygon();
            o.setInfo(new ObjectInfo(ObjectInfo.Types.ROAD));
            o.getInfo().name = res.getString(1);
            double[] points = (double[]) jGeo.getOrdinatesOfElements()[0];
            for(int i=0;i<points.length-2;i+=2) {
                ((ObjectPolygon)o).addPoint(new Point2D.Double(points[i],points[i+1]));
            }
            ((ObjectPolygon)o).setStrict(true);
            objects.add(o);
        }
        redraw();
        
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Editor(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItemProps;
    private javax.swing.JDialog jDialogProps;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemSave;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private javax.swing.JToggleButton jToggleButtonAdd;
    private javax.swing.JToggleButton jToggleButtonCircle;
    private javax.swing.JToggleButton jToggleButtonLine;
    private javax.swing.JToggleButton jToggleButtonPoint;
    private javax.swing.JToggleButton jToggleButtonPoly;
    // End of variables declaration//GEN-END:variables
    
}
